﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;

namespace TravelSite.Agent
{
    public partial class Agent : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetActiveLink();
            
            using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
            {
                lblUserName.Text = repo.Find(Convert.ToInt32(Context.Session["userId"])).FullName;
            }
        }

        protected void lbLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Abandon();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (recommend)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);

            FormsAuthentication.RedirectToLoginPage();
        }

        private void SetActiveLink()
        {
            string namePage = Page.GetType().FullName;

            tophlDashboard.Attributes["class"] = "dashboard_nav";
            tophlContactManagement.Attributes["class"] = "contacts_nav";
            tophlContracts.Attributes["class"] = "contracts_nav";
                                
            if (namePage == "ASP.agent_dashboard_aspx")
            {
                tophlDashboard.Attributes["class"] += " active active_dashboard";
            }
            else if (namePage == "ASP.agent_contactmanagement_aspx")
            {
                tophlContactManagement.Attributes["class"] += " active active_contacts";
            }
            else if (namePage == "ASP.agent_contracts_aspx")
            {
                tophlContracts.Attributes["class"] += " active active_contracts";
            }
        }
    }
}